package com.rachel.materialdemo.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.TextView;

import com.rachel.materialdemo.ItemDivider;
import com.rachel.materialdemo.R;
import com.rachel.materialdemo.adapter.HeaderAndFooterAdapter;
import com.rachel.materialdemo.bean.Cardone;
import com.rachel.materialdemo.common.RBaseAdapter;
import com.rachel.materialdemo.common.RBaseViewholder;

import java.util.ArrayList;
import java.util.List;

public class RecyclerViewActivity extends AppCompatActivity {
    private static final String TAG = "zsr";
    public static int VIEWTYPE_ONE = 1;
    public static int VIEWTYPE_TWO = 2;
    private RecyclerView mRecyclerView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_view);
        List<Cardone> mCardones = new ArrayList<>();

        for (int i = 0; i < 20; i++) {
            Cardone cardone = new Cardone();
            cardone.setViewtype(VIEWTYPE_ONE);
            cardone.setText("测试："+i);
            cardone.setResid(R.mipmap.beauty);
            mCardones.add(cardone);
        }
        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview);
        GridLayoutManager layoutManager = new GridLayoutManager(this,6);

        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.addItemDecoration(new ItemDivider());
        //绑定数据
        RBaseAdapter<Cardone> adapter = new RBaseAdapter<Cardone>(R.layout.cardview_item,mCardones) {
            @Override
            public void getConver(RBaseViewholder holder, Cardone data) {
                holder.setText(R.id.card_text,data.getText());
                holder.setImageResId(R.id.card_img,data.getResid());
            }
        };


        final HeaderAndFooterAdapter headerAndFooterAdapter = new HeaderAndFooterAdapter(adapter);
        TextView textView = new TextView(this);
        textView.setText("this is header test");
        headerAndFooterAdapter.addHeaderView(textView);


        layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                int viewtype = headerAndFooterAdapter.getItemViewType(position);
                Log.d(TAG, "getSpanSize: "+viewtype);
                if (viewtype == 10000){
                    return 6;
                }
                    return 3;
            }
        });

        mRecyclerView.setAdapter(adapter);

    }



}
